package com.moore.utils;

import com.alibaba.fastjson2.JSON;

import java.util.List;

public class AddressUtils {
    public static String getAddress(String id) {
        String sql = "select address from address where user_id = " + id;
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql, "address");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return JSON.toJSONString(list);
    }

    public static String insertAddress(String address, String id) {
        String sql = "insert into address(address,user_id) values(?,?)";
        int num;
        try {
            num = JDBCUtils.executeUpdate(sql, address, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            return "success";
        }
        return "fail";
    }

    public static String deleteAddress(String address, String id) {
        String sql = "delete from address where user_id = ? and address = ?";
        int num;
        try {
            num = JDBCUtils.executeUpdate(sql, id, address);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            return "success";
        }
        return "fail";
    }

    public static String updateAddress(String address, String oldAddress) {
        String sql = "update address set address = ? where address = ?";
        try {
           int num = JDBCUtils.executeUpdate(sql, address, oldAddress);
            if (num >= 1) {
                return "success";
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "fail";
    }
}
